use std::borrow::Cow;

use serde::Serialize;
use thiserror::Error;

pub type IoErr = std::io::Error;
pub type SerialErr = tokio_serial::Error;

#[derive(Debug, Error)]
pub enum AppErr {
    #[error("io:{0}")]
    Io(#[from] IoErr),

    #[error("serial:{0}")]
    Serial(#[from] SerialErr),

    #[error("other:{0}")]
    Other(Cow<'static, str>),
}

impl Serialize for AppErr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}
